package com.woniuxy01;

public class MyUtil {

    public int arraySearch(int[] a, int num) {
//        for (int i = 0; i < a.length; i++) {
//            if (a[i] == num) {
//                return i;
//            }
//        }
//
//        return -1;

        int index = -1;
        for (int i = 0; i < a.length; i++) {
            if (a[i] == num) {
                index = i;
                break;
            }
        }
        return index;
    }


    public int arrayBinarySearch(int[] a, int num) {
        int left = 0, right = a.length - 1, mid;
        while (left <= right) {
            mid = (left + right) / 2;
            if (a[mid] == num) {
                return mid;
            } else if (a[mid] < num) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }


    public void mySort1(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }

    public int[] mySort2(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        return a;
    }


    public boolean isZhiShu(int num) {
//        for(int i=2;i<num;i++){
//            if(num%i==0){
//                return false;
//            }
//        }
//        return true;

        boolean flag = true;
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }


    public int operator(int numA, int numB, String type) {
        int result = 0;
        switch (type) {
            case "+":
                result = numA + numB;
                break;
            case "-":
                result = numA - numB;
                break;
            case "*":
                result = numA * numB;
                break;
            case "/":
                if (numB != 0) {
                    result = numA / numB;
                    break;
                }else{
                    throw new RuntimeException("除数0");
                }
            default:
                throw new RuntimeException("无效运算符，无法计算");
        }
        return result;
    }

    public static void main(String[] args) {
        try {
            System.out.println(new MyUtil().operator(1, 0, "/"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
